package com.ly.juc;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname TestSemaphore
 * @Description TODO
 * @Date 2020/10/4 17:28
 * @Author 冷心影翼
 */
public class TestSemaphore {

	public static void main(String[] args) {
		semaphore();
	}

	public static void semaphore() {
		Park park = new Park();
		for(int i=0;i<20;i++) {
			new Thread(()->{
				park.stopCar();
				park.getOut();
			}).start();

		}
	}

}

class Park {
	int number = 10;

	Semaphore semaphore = new Semaphore(number);

	public  void stopCar() {
		try {
			System.out.println("停车，车位还剩余:"+(--number));
			semaphore.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public  void getOut() {
		System.out.println("开走，车位还剩余:"+(++number));
		semaphore.release();
	}
}